微信小程序代码体积压缩到最小

1.不要引用本地图片或者图标,最好是从服务器获取,远程 URL 地址加载图片。如果是图标的话,阿里的图标真的很好用,在小程序中使用也非常的方便。
2.优化代码逻辑,这个可能要求有一点高,减少不必要的代码,避免出现重复的代码。同时页面书写时候也要尽量避免不必要的组件嵌套,能用一个 view 做到的,就不要再多套一层 view。对减少代码尺寸和代码性能都是有好处的。
3.尽量的复用页面,判断逻辑写在页面中区分。正常的开发页面都要尽量分开便于维护,但是在小程序中多一个页面可就多生成4个文件呢。所以当你无法再优化和精简你的代码的时候,选择具有相同功能的页面尽量复用吧。毕竟鱼和熊掌不可兼得,谁叫微信有2M的大小限制呢。
4.使用工具压缩优化代码。使用一些前端工程化工具来处理我们的代码,比如使用 Gulp,结合一些功能插件,如:Uglify、CSS Nano、HTML min 等。使用这些工具,可以让我们的代码尺寸小上那么一大截(大约 20%~30%)。网上据说wepy自带的build命令压缩效果要比npm好,由于自己开发使用的就是wepy感觉一般般,其实和普通工程化工具没什么区别。

Jcompress 是一款基于哈夫曼编码和最小堆的无损压缩、解压缩小程序,支持任何格式的文件的压缩与解压缩。Jcompress 的源代码位于 Utility 的 repository 分类下的 Jcompress 目录,后续会在 Utility 下面增加其他一些实用的小程序,比如基于 socket 的文件断点下载小程序等等。Jcompress代码实现1. 最小代码实现最小堆排序算法基本上是按照严蔚敏版的算法来实现的,其具体功能这里不再赘述,仅列出代码,读者可以参考课本自行分析之。首先是heap_min_adjust,也就是调整堆,代码如下所示:int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end){       /**       ** check error for argument       */       if(huffman_node_array==NULL || data_start<0 || data_end<0 || data_end<data_start){           printf("heap_min_adjust: argument error\n");           exit(0);       }          if(data_end==data_start) return 1;       /**       ** the top of heap-min indicated by index data_start is the only element       ** which need to be adjusted to make a min-heap       */       HuffmanNode * current_data_tobe_adjust=huffman_node_array[data_start];       long current_indexof_data=data_start;          for(long cur=2*data_start;cur<=data_end;cur=2*cur){           if(curdata_8bit_count)>((huffman_node_array[cur 1])->data_8bit_count)){                   cur =1;               }           }           if((current_data_tobe_adjust->data_8bit_count)data_8bit_count))               break;           huffman_node_array[current_indexof_data]=huffman_node_array[cur];           current_indexof_data=cur;       }       huffman_node_array[current_indexof_data]=current_data_tobe_adjust;       /**       ** return 1 means everything is ok       */       return 1;   }接下来是heap_min_construct()的代码,此函数是通过不断的调用上面的调整堆的函数来达到堆排序的目的。int heap_min_construct(HuffmanNode **huffman_node_array, long array_size){       /** valid data start from index 1 not 0 */          /**       ** check error for argument       */        if(huffman_node_array==NULL || array_size256){           printf("heap_min_construct: argument error\n");           exit(0);        }           for(long HeapRoot=array_size/2; HeapRoot>=1;HeapRoot--){           heap_min_adjust(huffman_node_array,HeapRoot,array_size);        }        return 1;   }最后是heap_min_get2min()函数。int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size){          /**       ** check argument       **/        if(huffman_node_array==NULL){           printf("heap_min_get2min: argument error\n");           exit(0);        }          *min_first=huffman_node_array[1];       huffman_node_array[1]=huffman_node_array[*heap_size];       (*heap_size)-=1;       /** after we get the min data, we should again adjust the heap to make a min-heap */       heap_min_adjust(huffman_node_array,1,*heap_size);          *min_second=huffman_node_array[1];       huffman_node_array[1]=huffman_node_array[*heap_size];       (*heap_size)-=1;          /** the same as above*/       if(*heap_size>0){           heap_min_adjust(huffman_node_array,1,*heap_size);       }          return 1;   } 标签:Jcompress
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月落星河°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值